// 导入框架文件
import Vue from 'vue'
// 导入路由插件
import VueRouter from 'vue-router'
import LayoutView from '../LayoutView.vue'
// 使用路由插件
Vue.use(VueRouter)

// 直接加载主页
import HomeView from '@/views/HomeView.vue'
// import Index from '@/Index.vue'

// 配置路由
const routes = [
    {// 设置默认路由指向
        path: '/',
        // redirect: '@/Index.vue',
        redirect: '/home/recommand'
    },
    {
        path: '/',
        component: LayoutView,
        name: 'layout',
        children: [
            // {
            //     path: '/index',
            //     component: Index,
            //     name: 'index',
            //     meta: {
            //         // 开关
            //         isAuthenticated: true
            //     }
            // },
            // {
            //     // 设置默认路由指向
            //     path: '/app',
            //     component: () => import('@/App.vue'),
            //     name: 'app',
            //     meta: {
            //         // 开关
            //         isAuthenticated: true
            //     }
            // },
            {
                path: '/home',
                component: () => import('@/views/HomeView.vue'),
                name: 'home',
                meta: {
                    // 开关
                    isAuthenticated: true
                },
                children: [
                    {
                        // 子路由的path配置不需要'/'
                        path: '/home/recommand',
                        name: 'recommand',
                        component: () => import('../views/pages/RecommandView.vue')
                    },
                    {
                        path: '/home/selected',
                        name: 'selected',
                        component: () => import('../views/pages/SelectedView.vue')
                    },
                    {
                        path: '/home/new',
                        name: 'new',
                        component: () => import('../views/pages/NewView.vue')
                    },
                    {
                        path: '/home/theme',
                        name: 'theme',
                        component: () => import('../views/pages/ThemeView.vue')
                    },
                    {
                        path: '/home/head',
                        name: 'head',
                        component: () => import('../views/pages/HeadView.vue')
                    },
                    {
                        path: '/backto',
                        name: 'head',
                        component: () => import('../views/pages/BackTo.vue')
                    }
                ]
            },
            {
                path: '/my',
                component: () => import('@/views/MyView.vue'),
                name: 'my',
                meta: {
                    // 开关
                    isAuthenticated: false
                }
                
            },
            {
                path: '/money',
                component: () => import('@/views/MoneyView.vue'),
                name: 'money',
                meta: {
                    // 开关
                    isAuthenticated: false
                }
            },
            {
                path: '/task',
                component: () => import('@/views/TaskView.vue'),
                name: 'task',
                meta: {
                    // 开关
                    isAuthenticated: true
                },

                // 路由独享守卫
                // beforeEnter: (to, from, next) => {
                //     // ...
                //     let id = localStorage.getItem('id');
                //     console.log(id);
                //     if (id == 20240920) {
                //         next()
                //     }
                //     else {
                //         if (confirm('您的身份信息不正确,请先登录再访问')) {
                //             next('/login')
                //         }
                //         else {
                //             next();
                //         }
                //     }
                // }
            },]
    },
    {
        path: '/login',
        // 延迟加载
        component: () => import('@/views/LoginView.vue'),
        name: 'login',
        meta: {
            // 开关
            isAuthenticated: true
        }
    },
    {
        path: '/register',
        component: () => import('@/views/RegisterView.vue'),
        name: 'register',
        meta: {
            // 开关
            isAuthenticated: false
        }
    },
    {
        path: '/wrapper',
        component: () => import('@/views/WrapperView.vue'),
        name: 'wrapper',
        meta: {
            // 开关
            isAuthenticated: true
        }
    },
    {
        path: '/register',
        component: () => import('@/views/RegisterView.vue'),
        name: 'register',
        meta: {
            // 开关
            isAuthenticated: false
        }
    },
    {
        path: '/home/more',
        component: () => import('@/views/MoreView.vue'),
        name: 'more',
        meta: {
            // 开关
            isAuthenticated: false
        },
        children: [
            {
                path: '/home/more/fengjing',
                name: 'fengjing',
                component: () => import('../views/pages/FengjingView.vue')
            },
            {
                path: '/home/more/meitu',
                name: 'meitu',
                component: () => import('../views/pages/MeituView.vue')
            },
            {
                path: '/home/more/zuixin',
                name: 'zuixin',
                component: () => import('../views/pages/ZuixinView.vue')
            },
            {
                path: '/home/more/wenzi',
                name: 'wenzi',
                component: () => import('../views/pages/WenziView.vue')
            },
        ]
    },
    {
        path: '/my/xiazai',
        component: () => import('@/views/pages/XiazaiView.vue'),
        name: 'xiazai',
},
    {
        path: '/search',
        component: () => import('@/views/SearchView.vue'),
        name: 'search',
        meta: {
            // 开关
            isAuthenticated: true
        },
    },
    ,]

// 创建路由实例对象
const router = new VueRouter({
    routes,
});

// 全局路由守卫
// 这个是针对所有路由都做拦截的
// router.beforeEach((to, from, next) => {
//     console.log("to:",to)
//     console.log("from:",from)
//     // 获取访问条件
//     let { isAuthenticated } = to.meta
//     if (to.name !== 'login' && !isAuthenticated) next({ name: 'login' })
//     // 如果用户未能验证身份，则 `next` 会被调用两次
//     next()
// })


// 保留这个实例
export default router